<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4: 
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@rg.by>	                  |
// | Copyright (c) 2004 Max Baryshnikov                                   |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 07.11.05 (dd/mm/yy)

/**
 * Sorting of an array by index (ascend)
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_indexasc')) {
   function cmp_indexasc($a, $b) {

      global $sapi_obj;

      $index = $sapi_obj->env["index"];

      if($index=='ID')

         return ($a[$index] > $b[$index]) ? -1 : 1;

      else if ($index=='DATE'){

         $date_a=explode('/',$a[$index]);

         $date_b=explode('/',$b[$index]);

         $a_index=mktime (0,0,0, $date_a[1], $date_a[0], $date_a[2]) . $a["DATE_CREATE"]; //Sort by DATE. If DATEs is equal, then sort by DATE_CREATE.

         $b_index=mktime (0,0,0, $date_b[1], $date_b[0], $date_b[2]) . $b["DATE_CREATE"];

         return strcmp($a_index,$b_index);

      }

      else

         return strcmp($a[$index], $b[$index]);

   }
} 
/**
 * Sorting of an array by index (descend)
 * 
 * @param string $b
 * @param string $a
 * @return boolean result
 */
if (!function_exists('cmp_indexdesc')) {
   function cmp_indexdesc($a, $b) {

      global $sapi_obj;

      $index = $sapi_obj->env["index"];

      if($index=='ID')

         return ($a[$index] > $b[$index]) ? 1 : -1;

      else if ($index=='DATE'){

         $date_a=explode('/',$a[$index]);

         $date_b=explode('/',$b[$index]);

         $a_index=mktime (0,0,0, $date_a[1], $date_a[0], $date_a[2]) . $a["DATE_CREATE"]; //Sort by DATE. If DATEs is equal, then sort by DATE_CREATE.

         $b_index=mktime (0,0,0, $date_b[1], $date_b[0], $date_b[2]) . $b["DATE_CREATE"];
         return strcmp($b_index,$a_index);

      }

      else

         return strcmp($b[$index],$a[$index]);

   }
}
/**
 * Sorting of an  array ([TITLE] asc)
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_nameasc')) {
	function cmp_nameasc($a, $b) {
		if($a["TITLE"] == $b["TITLE"]) return 0;
		return ($a["TITLE"] < $b["TITLE"]) ? -1 : 1;
	}
}
/**
 * Sorting of an array ([TITLE] desc)
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_namedesc')) {
	function cmp_namedesc($a, $b) {
		if($a["TITLE"] == $b["TITLE"]) return 0;
		return ($a["TITLE"] > $b["TITLE"]) ? -1 : 1;
	}
}
/**
 * Sorting of an array by creation date (asc)
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_datecasc')) {
	function cmp_datecasc($a, $b) {
		if($a["DATE_CREATE"] == $b["DATE_CREATE"]) return 0;
		return ($a["DATE_CREATE"] < $b["DATE_CREATE"]) ? -1 : 1;
	}
}
/**
 * Sorting of an array by creation date (desc)
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_datecdesc')) {
	function cmp_datecdesc($a, $b) {
		if($a["DATE"] == $b["DATE"]) return 0;
		preg_match_all("/(\d\d)\/(\d\d)\/(\d\d\d\d)/is", $a["DATE"], $mathes);
		$a_date = $mathes[3][0].$mathes[2][0].$mathes[1][0];
		preg_match_all("/(\d\d)\/(\d\d)\/(\d\d\d\d)/is", $b["DATE"], $mathes);
		$b_date = $mathes[3][0].$mathes[2][0].$mathes[1][0];		
		return ($a_date > $b_date) ? -1 : 1;
	}
}
/**
 * Random sorting of an array 
 * 
 * @param string $a
 * @param string $b
 * @return boolean result
 */
if (!function_exists('cmp_random')) {
	function cmp_random($a, $b) {
		srand ((double) microtime() * 1000000);
		$randval = rand(-1,1);
		return $randval;
	}
}

?>